/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class FileController extends AbstractController {
    
    public FileController() {
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
                
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            case 1: {
                uploadFile(mav, map, request);
            }
            default: {
                uploadFile(mav, map, request);
                break;
            }
        }        
        mav.addObject("model", map);
        return mav;
    }
        
    private void uploadFile(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            
            try {
                List items = upload.parseRequest(request);
                Map params = getMultipartFormParams(items);
                
                
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();

                    if (!item.isFormField()) {
                        String fileName = item.getName();
                        
                        if (!StringUtils.isEmpty(fileName)) {                            
                            String root = getServletContext().getRealPath("/");
                            root = "D:/CX1/uploads/img";
                            File path = new File(root);
                            if (!path.exists()) {
                                boolean status = path.mkdirs();
                            }

                            File uploadedFile = new File(path + "/" + fileName);
                            if (!uploadedFile.exists()) {                            
                                System.out.println(uploadedFile.getAbsolutePath());
                                item.write(uploadedFile);                                
                            }
                        }
                    }
                    else {
                        System.out.println("CX:" + item.getString());
                    }
                }
            } 
            catch (FileUploadException e) {
                e.printStackTrace();
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private Map getMultipartFormParams(List<FileItem> items) {
        Map map = new HashMap();
        for (FileItem item : items ) {
            if (item.isFormField()) {
                map.put(item.getFieldName(), item.getString());
            }
        }
        return map;
    }
}
